using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Rendering;

namespace Nirvana
{
    public class SerializedLight : SerializedGameObject
    {
        public int LightType;
        public float Range;
        public Color Color;
        public float Intensity;
        public float BounceIntensity;
        public int Shadows;
        public float ShadowStrength;
        public int ShadowResolution;
        public float ShadowBias;
        public float ShadowNormalBias;
        public float ShadowNearPlane;
        public float SpotAngle;
        public int RenderMode;
        public int CullingMask;
        public int LightShadowCasterMode;

        private Light _light;

        protected override void OnWrite(BinaryWriter writer)
        {
            writer.Write(LightType);
            writer.Write(Range);
            writer.Write(Color);
            writer.Write(Intensity);
            writer.Write(BounceIntensity);
            writer.Write(Shadows);
            writer.Write(ShadowStrength);
            writer.Write(ShadowResolution);
            writer.Write(ShadowBias);
            writer.Write(ShadowNormalBias);
            writer.Write(ShadowNearPlane);
            writer.Write(SpotAngle);
            writer.Write(RenderMode);
            writer.Write(CullingMask);
            writer.Write(LightShadowCasterMode);
        }

        protected override void OnRead(BinaryReader reader)
        {
            LightType = reader.ReadInt32();
            Range = reader.ReadSingle();
            Color = reader.ReadColor();
            Intensity = reader.ReadSingle();
            BounceIntensity = reader.ReadSingle();
            Shadows = reader.ReadInt32();
            ShadowStrength = reader.ReadSingle();
            ShadowResolution = reader.ReadInt32();
            ShadowBias = reader.ReadSingle();
            ShadowNormalBias = reader.ReadSingle();
            ShadowNearPlane = reader.ReadSingle();
            SpotAngle = reader.ReadSingle();
            RenderMode = reader.ReadInt32();
            CullingMask = reader.ReadInt32();
            LightShadowCasterMode = reader.ReadInt32();
        }

        public void CollectData(Light light)
        {
            LightType = (int) light.type;
            Range = light.range;
            Color = light.color;
            Intensity = light.intensity;
            BounceIntensity = light.bounceIntensity;
            Shadows = (int) light.shadows;
            ShadowStrength = light.shadowStrength;
            ShadowResolution = (int) light.shadowResolution;
            ShadowBias = light.shadowBias;
            ShadowNormalBias = light.shadowNormalBias;
            ShadowNearPlane = light.shadowNearPlane;
            SpotAngle = light.spotAngle;
            RenderMode = (int) light.renderMode;
            CullingMask = light.cullingMask;
            LightShadowCasterMode = (int) light.lightShadowCasterMode;
        }

        public void SetData(Light light)
        {
            light.type = (LightType) LightType;
            light.range = Range;
            light.color = Color;
            light.intensity = Intensity;
            light.bounceIntensity = BounceIntensity;
            light.shadows = (LightShadows) Shadows;
            light.shadowStrength = ShadowStrength;
            light.shadowResolution = (LightShadowResolution) ShadowResolution;
            light.shadowBias = ShadowBias;
            light.shadowNormalBias = ShadowNormalBias;
            light.spotAngle = SpotAngle;
            light.renderMode = (LightRenderMode) RenderMode;
            light.cullingMask = CullingMask;
            light.lightShadowCasterMode = (LightShadowCasterMode) LightShadowCasterMode;
        }

        public override void Load()
        {
            if (Node != null)
            {
                if (_light == null)
                {
                    _light = Node.gameObject.GetComponent<Light>();
                    if (_light == null)
                    {
                        _light = Node.gameObject.AddComponent<Light>();
                        SetData(_light);
                    }

                    if (!_light.enabled)
                    {
                        _light.enabled = true;
                    }
                }
            }
        }

        public override void UnLoad()
        {
            if (Node != null && _light != null)
            {
                if (_light.enabled)
                {
                    _light.enabled = false;
                }
            }
        }

        protected override void OnClear()
        {
            _light = null;
        }
    }
}